<?php
Class FT_Site {
	private $info = array();
	private $url = array();
	private $blogs = array();
	private $blog = null;
	private $barra_superior = null;
	private $widgets = array();
	private $eventos = null;
	
	/* CONSTRUCTOR */
	 
	public function FT_Site() {		
		$this->FT_Crear_Info();
		$this->FT_Crear_URL();
		$this->FT_Crear_Blogs();
		$this->FT_Crear_Barra_Superior();
		$this->FT_Crear_Widgets();
		$this->FT_Crear_Eventos();
		
		$this->FT_Hooks();
		$this->FT_CSS_JS();
	}
	
	/* METODOS PRIVADOS */
	
	/* Aplica Hooks al sitio */
	
	private function FT_Hooks() {
		add_filter('wp_head', 'FT_Agregar_CSS_JS');
		
		// Quitando los elementos extras que agregan Buddypress y otros plugins
		remove_action('admin_footer', 'bp_core_admin_bar');
		remove_action('admin_head', 'bp_core_admin_menu_icon_css');
		remove_action('admin_menu', 'bp_core_add_admin_bar_css');
		remove_action('admin_menu', 'bp_core_add_admin_bar_js');
		remove_action('admin_menu', 'bp_core_admin_bar_css');
		remove_action('bp_adminbar_logo', 'bp_adminbar_logo');
		remove_action('bp_adminbar_menus', 'bp_adminbar_login_menu', 2);
		remove_action('bp_adminbar_menus', 'bp_adminbar_account_menu', 4);
		remove_action('bp_adminbar_menus', 'bp_adminbar_blogs_menu', 6);
		remove_action('bp_adminbar_menus', 'bp_adminbar_notifications_menu', 8);
		remove_action('bp_adminbar_menus', 'bp_adminbar_authors_menu', 12);
		remove_action('bp_adminbar_menus', 'xprofile_setup_adminbar_menu', 20);
		remove_action('bp_adminbar_menus', 'bp_adminbar_random_menu', 100);
		remove_action('wp_footer', 'bp_core_override_adminbar_css');
	}
	
	/* Anexa CSS y scripts de Javascript al sitio */
	
	private function FT_CSS_JS() {
		$css = array(
			array(
				'nombre' => 'ft_first_tuesday-css',
				'src' => FT_PLUGIN_URL.'/css/style.css'
			)
		);
		
		$js = array(
			array(
				'nombre' => 'ft_first_tuesday-js',
				'src' => FT_PLUGIN_URL.'/js/functions.js',
				'dependencia' => array('jquery')
			)
		);
		
		FT_Agregar_CSS_JS($css, $js);
	}
	
	/* Crea la info basica del sitio */
	
	private function FT_Crear_Info() {
		global $current_site;
		
		$this->info = array(
			'id' => $current_site->id,
			'name' => get_blog_option($current_site->id, 'blogname'),
			'domain' => $current_site->domain
		);
	}
	
	/* Crea las URL del sitio */
	
	private function FT_Crear_URL() {
		$url_base_aux = 'http://'.$this->info['domain'];
		
		$this->url = array(
			'base' => $url_base_aux,
			'url_'.BP_BLOGS_SLUG => $url_base_aux.'/'.BP_BLOGS_SLUG.'/',
			FT_ORGANIZADORES_SLUG => $url_base_aux.'/'.FT_ORGANIZADORES_SLUG.'/',
			FT_CONTACTAR_SLUG => $url_base_aux.'/'.FT_CONTACTAR_SLUG.'/',
			FT_ACERCA_SLUG => $url_base_aux.'/'.FT_ACERCA_SLUG.'/'
		);
	}
	
	/* Crea los blogs (ciudades) del sitio e identifica el blog (ciudad) actual */
	
	private function FT_Crear_Blogs() {
		$blogs_aux = get_blog_list(0, 'all');
		
		foreach($blogs_aux as $blog_aux) :
			$blog= new FT_Blog($blog_aux['blog_id']);
			$this->blogs[strtolower($blog->FT_Get_Info('name'))] = $blog;
			if($blog->FT_Get_Info('current'))
				$this->blog = $blog;
		endforeach;
		
		ksort($this->blogs);
		
		foreach($this->blogs as $blog_aux) :
			$blogs_aux_aux[$blog_aux->FT_Get_Info('id')] = $blog_aux;
		endforeach;
		
		$this->blogs = $blogs_aux_aux;
		
		$this->info['total_blogs'] = count($this->blogs); 
	}
	
	/* Crea la barra superior */
	
	private function FT_Crear_Barra_Superior() {
		$this->barra_superior = new FT_Top_Bar();
	}
	
	/* Crea los widgets */
	
	private function FT_Crear_Widgets() {
		$this->widgets = new FT_Widgets();
	}
	
	/* Crea los eventos */
	
	private function FT_Crear_Eventos() {
		$this->eventos = new FT_Events();
	}
	
	/* METODOS PUBLICOS */
	
	/* Devuelve la info o parte de la info basica del sitio */
	
	public function FT_Get_Info($info_name = '') {
		if(empty($info_name))
			return $this->info;
		else
			return $this->info[strtolower($info_name)];
	}
	
	/* Devuelve las URL del sitio */
	
	public function FT_Get_URL($url_name = '') {
		if(empty($url_name))
			return $this->url;
		else
			return $this->url[strtolower($url_name)];
	}
	
	/* Devuelve el o los blogs (ciudades) del sitio */
	
	public function FT_Get_Blogs($blog_id = null) {
		if(is_null($blog_id))
			return $this->blogs;
		else
			return $this->blogs[$blog_id];
	}
	
	/* Establece el blog (ciudad) actual */
	
	public function FT_Set_Current_Blog($blog = null) {
		if(!is_null($blog)) :
			if(is_numeric($blog)) :
				$this->blog = $this->FT_Get_Blogs($blog);
			elseif(is_object($blog) && get_class($blog) == 'FT_Blog') :
				$this->blog = $blog;
			endif;
		elseif(!isset($this->blog)) :
			foreach($this->blogs as $blog_aux) :
				if($blog_aux->FT_Get_Info('current')) :
					$this->blog = $blog_aux;
					break;
				endif;
			endforeach;
		endif;
	}
	
	/* Devuelve el blog (ciudad) actual del sitio */
	
	public function FT_Get_Current_Blog() {
		return $this->blog;
	}
}
?>